<?php

require_once("functions.php");
$db = connect_db();

$out = get_header();
$sql_var = '';
if (isset($_POST['var'])) {
	$sv = "'%" . mysql_real_escape_string($_POST['var']) . "%'";
	$sql_var = " WHERE variable LIKE $sv OR extra_variable LIKE $sv "; 
}

$sql = "select *  from languages order by language ASC";
$res = mysql_query($sql, $db);
$lang = array();
while(($row = mysql_fetch_array($res)) !== FALSE) 
	$lang[$row['id']] = $row['language'];

$sql= "SELECT variable_id, language_id, value,value2, variable, extra_variable FROM fields JOIN `values` ON fields.id=variable_id $sql_var";
$res = mysql_query($sql, $db);
if ($res == FALSE) die ("Invalid query $sql");
$vals = array();
while (($row = mysql_fetch_array($res)) !== FALSE) {
	$vals[$row['variable_id']]['variable'] = $row['variable'];
	$vals[$row['variable_id']]['extra_variable'] = $row['extra_variable'];
	$vals[$row['variable_id']]['languages'][$row['language_id']] = array (0=>$row['value'], 1=>$row['value2']);
}
//echo '<pre>';
//var_dump($vals);
//echo '</pre>';

$out .= generate_menu();

$out .= "<form method=\"post\" action=\"\">";
$out .= "<input type=\"text\" name=\"var\" size=\"20\">";
$out .= "<input type=\"submit\" name=\"submit\" value=\"Search\">";

$out .= "</form>";

$out .= "<h2>Fields overview</h2>\n";
$out .= "<table>\n";
$out .= "<tr><th>Variable</th>";

foreach($lang as $l)
	$out .= "<th>" . ucwords($l) . "</th>";
$out .= "</tr>\n";
	
foreach ($vals as $id=>$r) {

	$out .= '<tr valign="top"><td>'. "<a href=\"edit_values.php?action=edit&amp;id=$id\">" .
		'$LN["' . $r['variable'] . '"]' .  (($r['extra_variable'] != '') ? ('["' . $r['extra_variable']. '"]') : '') . '</a></td>';
	foreach ($lang as $i=>$l) {
		$out .= '<td>';
		if (isset($r['languages'][$i][0]))	{		
			$out.=  $r['languages'][$i][0];
		}
		if ((isset($r['languages'][$i][1])) && ($r['languages'][$i][1] != NULL)){
			$out .= '==><br/>';
			$out .= $r['languages'][$i][1];
		}
		$out .= '</td>';
	}
	$out .=  "</tr>\n";
}
$out .="</table>\n";

$out .= "<a href=\"edit_field.php?id=new\">add field</a>";

$out .= get_footer();


echo $out;

?>
